package com.ychen.ruijee.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ychen.ruijee.bean.ShoppingCart;
import com.ychen.ruijee.common.BaseContext;
import com.ychen.ruijee.common.R;
import com.ychen.ruijee.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ychen@gotop.cn
 * @date 2024-06-08 14:29
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;

    /**
     * 添加到 购物车中
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    private R<ShoppingCart> saveShopoingCart (@RequestBody ShoppingCart shoppingCart){
        ShoppingCart flag = shoppingCartService.saveShopoingCart(shoppingCart);
        return flag != null ? R.success(flag):R.error("添加购物车失败");
    }

    /**
     * 查看购物车信息
     * @return
     */
    @GetMapping("/list")
    private R<List<ShoppingCart>> listShoppingCart(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,currentId);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(wrapper);
        return R.success(shoppingCartList);
    }

    /**
     * 减少购物车中的数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    private R<String> subShoppingCart(@RequestBody ShoppingCart shoppingCart){
        boolean flag  = shoppingCartService.subShoppingCart(shoppingCart);
        return flag ? R.success("购物车修改成功"):R.error("购物车修改失败");
    }

    /**
     * 清空购物车中信息
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> deleteShoppingCart(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,currentId);
        boolean flag = shoppingCartService.remove(wrapper);
        return flag ? R.success("清空购物车成功"):R.error("清空购物车失败");
    }

}
